/* UVa- 10189
 * Author- Ajeet Singh
 * http://uva.onlinejudge.org
 * id- ajeet.singh82
 * Contact- ajeet.singh82@gmail.com
 *
 * 
 * Algorithm: 
 *
 *
 * Build:
 * ...
 */
#include <iostream>
#include <iterator> 
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <locale>
#include <queue>
#include <stack>
using namespace std;

typedef pair<unsigned short, unsigned short> point;
unsigned short M = 0, N = 0;
vector<string> dc;

inline unsigned short  Max(unsigned short x, unsigned short y) {
	return x ^ ((x ^ y) & -(x < y));
}

inline unsigned short Min(unsigned short x, unsigned short y) {
	return y ^ ((x ^ y) & -(x < y));
}


inline char test(const point& T) {
	return dc[T.second][T.first];
}

void process_I(unsigned short R, unsigned short C){
	dc.clear();
	string pixelrow(R, 'O');
	while(C-- > 0) dc.push_back(pixelrow);
}

void apply_color(point& A, point& B, char C) {
	if (A == B) {
		dc[A.second][A.first] = C;
	} else if (A.first == B.first) {
		while(A.second <= B.second) {
			dc[A.second++][A.first] = C;
		}
	} else {
		string replacement(B.first-A.first+1, C);
		while(A.second <= B.second) {
			dc[A.second++].replace(A.first, replacement.size(), replacement);
		}
	
	}
}

void fill_color(const point& A, char P, char C) {
	bool spanLeft, spanRight;
	stack<point> S;
	S.push(A);
	while(S.empty() == false)
	{    
		point n = S.top();
		short x, y;
		x = n.first;
		y = n.second;
		S.pop();
		while(y >= 0 && dc[y][x] == P) 
			y--;
		y++;
		spanLeft = spanRight = false;
		while(y < N && dc[y][x] == P )
		{
			dc[y][x] = C;
			if((!spanLeft) && x > 0 && dc[y][x-1] == P) {
				S.push(point(x-1, y));
				spanLeft = true;
			}
			else if(spanLeft && x > 0 && dc[y][x-1] != P) {
				spanLeft = false;
			}
			if((!spanRight) && x < M-1 && dc[y][x+1] == P) {
				S.push(point(x+1, y));
				spanRight = true;
			}
			else if(spanRight && x < M-1 && dc[y][x + 1] != P) {
				spanRight = false;
			} 
			y++;
		}
	}
}

bool process_cmd(const string& cmdline) {
	bool result = false;
	stringstream tokens(stringstream::in | stringstream::out);
	tokens << cmdline;
	char cmd;
	tokens >> cmd;
	switch(cmd) {
		case 'I':
			tokens >> M >> N;
			if(M > 0 && M <= 250 && N> 0 && N <= 250)
				process_I(M, N);
			else 
				M=N=0;
			break;
		case 'C':
			if(M > 0 && M <= 250 && N> 0 && N <= 250)
				process_I(M, N);
			break;
		case 'L':
			{
				unsigned short X, Y;
				char C;
				tokens >> X >> Y >> C;
				X--; Y--;
				if(C < 'A' || C > 'Z')
					break;
				if(X < M && Y < N) {
					point A(X, Y);
					apply_color(A, A, C);
				}
			}
			break;
		case 'V':
			{
				unsigned short X, Y1, Y2;
				char C;
				tokens >> X >> Y1 >> Y2 >> C;
				X--; Y1--; Y2--;
				if(C < 'A' || C > 'Z')
					break;
				if(X < M && Y1 < N && Y2 < N) {
					point A(X, min(Y1, Y2)), B(X, max(Y1, Y2));
					apply_color(A, B, C);
				}
			}
			break;
		case 'H':
			{
				unsigned short X1, X2, Y;
				char C;
				tokens >> X1 >> X2 >> Y >> C;
				X1--; X2--; Y--;
				if(C < 'A' || C > 'Z')
					break;
				if(X1 < M && X2 < M && Y < N) {
					point A(min(X1, X2), Y), B(max(X1, X2), Y);
					apply_color(A, B, C);
				}
			}
			break;
		case 'K':
			{
				unsigned short X1, X2, Y1, Y2;
				char C;
				tokens >> X1 >> Y1 >> X2 >> Y2 >> C;
				X1--; X2--; Y1--; Y2--;
				if(C < 'A' || C > 'Z')
					break;
				if(X1 < M && X2 < M && Y1 < N && Y2 < N) {
					if(X1 > X2) swap(X1, X2);
					if(Y1 > Y2) swap (Y1, Y2);
					point A(X1, Y1), B(X2, Y2);
					apply_color(A, B, C);

				}
			}
			break;
		case 'F':
			{
				unsigned short X, Y;
				char C;
				tokens >> X >> Y >> C;
				X--; Y--;
				if(C < 'A' || C > 'Z')
					break;
				if(X < M && Y < N) {
					point A(X, Y);
					if(test(A) != C) {
						fill_color(A, test(A), C);
					}
				}
			}
			break;
		case 'S':
			{	
				string filename;
				tokens >> filename;
				cout << filename << "\n";
				copy(dc.begin(), dc.end(), ostream_iterator<string>(cout, "\n"));
			}
			break;
		case 'X':
			result= true;
			break;
		default:
			break;
	}
	return result;
}

/* main
 * */
int main() {
	std::ios_base::sync_with_stdio (true);
	bool exit = false;
	while ( exit == false ) {
		string cmdline;
		getline(cin, cmdline);
		exit = process_cmd(cmdline);
	}
	return 0;
}
